#ifndef _JPVEC2_H_
#define _JPVEC2_H_

#include <math.h>

class JPVec2
{
public:

	float x, y; // 2D vector coordinates

	JPVec2 (float newX=0.0f, float newY=0.0f) : x(newX), y(newY) {}

	//----------------------------------------
	// public api
	//----------------------------------------
	/*
	* returns the dot product between this and v
	*/
	float dot(const JPVec2 &v) const;

	/*
	* returns the cross product between this and v
	*/
	float cross(const JPVec2 &v) const;

	/*
	* returns the length of the vector
	*/
	float magnitude() const;

	/*
	* returns the sqr length ofthis vector
	*/
	float sqrMagnitude() const;

	/*
	* Normalises this vector to have length 1
	*/
	void normalise();

	/*
	* returns a normalised version of this vector
	*/
	JPVec2 getNormalised() const;

	//----------------------------------------
	// operator overloads
	//----------------------------------------
	// = operator
	JPVec2 operator =(const JPVec2 &v)
	{
		x = v.x; y = v.y;
		return *this;
	}
	// + operator
	JPVec2 operator +(const JPVec2 &v) const	{ return JPVec2(v.x+x, v.y+y); }
	// - operator
	JPVec2 operator -(const JPVec2 &v) const	{ return JPVec2(x-v.x, y-v.y); }
	// * operator
	JPVec2 operator *(float n) const			{ return JPVec2(x*n, y*n); }
	// * vector operator
	JPVec2 operator *(const JPVec2 &v) const	{ return JPVec2(x*v.x, y*v.y);}
	// / operator
	JPVec2 operator /(float n) const			{ return JPVec2(x/n, y/n); }
	// / vector operator
	JPVec2 operator /(const JPVec2 &v) const	{ return JPVec2(x/v.x, y/v.y); }
	// - operator
	JPVec2 operator -() const					{ return JPVec2(-x,-y); }
	// += operator
	JPVec2& operator +=(const JPVec2 &v)
	{
		x+=v.x; y+=v.y;
		return *this;
	}
	// -= operator
	JPVec2& operator -=(const JPVec2 &v)
	{
		x-=v.x; y-=v.y;
		return *this;
	}
	// *= operator
	JPVec2& operator *=(float n)
	{
		x*=n; y*=n;
		return *this;
	}
	// *= vector operator
	JPVec2& operator *=(const JPVec2 &v)
	{
		x*=v.x; y*=v.y;
		return *this;
	}
	// /= operator
	JPVec2& operator /=(float n)
	{
		x/=n; y/=n;
		return *this;
	}
	// /= vector operator
	JPVec2& operator /=(const JPVec2 &v)
	{
		x/=v.x; y/=v.y;
		return *this;
	}
	// [] operator
	float& operator [](int i)
	{
		// TODO: assert if i < 0 || i >1
		if(i<0||i>1) i = 0;

		return *(&x+i);
	}

	// const [] operator
	const float& operator [](int i) const
	{
		// TODO: assert if i < 0 || i >1
		if(i<0||i>1) i = 0;

		return *(&x+i);
	}

	//----------------------------------------
	// statics 
	//----------------------------------------

	/*
	 * unit vectors along each axis
	 */
	static const JPVec2 JPVec2::vecX;
	static const JPVec2 JPVec2::vecY;
};

#endif // _JPVEC2_H_